package com.sskj.gcc.ui.fragment;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.gcc.R;
import com.sskj.gcc.presenter.AssetFragmentPresenter;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.m.bean.AseetListBean;
import com.sskj.gocoin.utils.NumberUtil1;
import com.sskj.gocoin.v.activity.CapitalDetailsActivity;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;

import java.text.DecimalFormat;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_ASSET)
public class AssetFragment extends BaseFragment<AssetFragmentPresenter> {
    @BindView(R2.id.asset_recyclerView)
    RecyclerView assetRecyclerView;
    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    private BaseAdapter<AseetListBean.AseetBean> mAdapter;
    DecimalFormat mFormat = new DecimalFormat("0.0000");

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset;
    }

    @Override
    protected AssetFragmentPresenter getPresenter() {
        return new AssetFragmentPresenter();
    }

    @Override
    public void initData() {
        ClassicsHeader classicsHeader = new ClassicsHeader(getActivity());
        classicsHeader.setBackgroundColor(getResources().getColor(com.sskj.gocoin.R.color.background));
        smartRefresh.setRefreshHeader(classicsHeader);
        smartRefresh.setOnRefreshListener(refreshLayout -> {
            smartRefresh.finishRefresh();
//            mPresenter.upDateUserInfo(MyAppLication.getMobile());
            mPresenter.requestUserAssetList(true);
        });
        assetRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        assetRecyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 2))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(getActivity(), 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(getActivity(), 30))
                .setDividerColor(ContextCompat.getColor(getActivity(), com.sskj.gocoin.R.color.fabi_1c1)));
        mAdapter = new BaseAdapter<AseetListBean.AseetBean>(com.sskj.gocoin.R.layout.item_asset_manage, null, assetRecyclerView) {
            @Override
            public void bind(ViewHolder holder, AseetListBean.AseetBean item) {
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_type, item.pname);
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_available_balance, mFormat.format(item.usable));
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_frozen_capital, mFormat.format(item.frost));
                holder.setText(com.sskj.gocoin.R.id.item_asset_manage_cny, NumberUtil1.keepHalf(item.cny, 2));
                holder.setOnClickListener(com.sskj.gocoin.R.id.item_asset_manage_record, v -> {//账户明细
                    startActivity(new Intent(getActivity(), CapitalDetailsActivity.class)
                            .putExtra(Constants.INTENT_ASSET_ID, item.pid)
                            .putExtra(Constants.COIN_TYPE, item.pname));
                });
                holder.setText(com.sskj.gocoin.R.id.available_text, App.INSTANCE.getString(com.sskj.gocoin.R.string.fabimineJavaAssetManagerActivity2) + "(" + item.pname + ")");
                holder.setText(com.sskj.gocoin.R.id.freeze_text, App.INSTANCE.getString(com.sskj.gocoin.R.string.fabimineJavaAssetManagerActivity3) + "(" + item.pname + ")");
            }
        };

    }

    public void onDataSuccess(AseetListBean data) {
        mAdapter.setNewData(data.asset);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.requestUserAssetList(true);
    }

}
